<?php require ('setcookie.html'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="de">
<head>
    <title>MP3 Vokabeltrainer - Download - Lingomoto</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="MP3 Vokabeltrainer - Download - Sprachen lernen" />
    <meta name="language" content="de" />
    <link href="style.css" type="text/css" rel="stylesheet" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
</head>

<body id="trainer">
<?php
$_SESSION['logini'] = isset($_SESSION['logini']) ? $_SESSION['logini'] : false;
// read configuration
require ('konfiguration.html');
// read user-data
require ('nutzer.html');
// read header
$link = "download.html";
$langen = 'download.html';
$langes = 'download.html';
$langru = 'download.html';
require ('kopf.html');
// <!-- horizontal column -->
echo '<div>';
require ('horizontal.html');
echo '</div>';
// <!-- Left Column -->
echo '<div id="left_col">';
require ('vertikal.html');
echo '</div>';

// <!-- Content -->
echo '<div id="content">';
echo '<div id="block5">';

// request for login
if ($_SESSION['logini']== false) {
    require_once ('loginformular.html'); 
}

// if new password, request for change of password
elseif ( $newpass == 1 )
{
$passwo = isset($_REQUEST['passwo']) ? $_REQUEST['passwo'] : '1';
$passwo1 = isset($_REQUEST['passwo1']) ? $_REQUEST['passwo1'] : '1';
$passwo2 = isset($_REQUEST['passwo2']) ? $_REQUEST['passwo2'] : '1';
$ok='';
require_once ('neues-passwort.html');
}

else 
{
    // initialize variables
    $action = '';
    $break = isset($_REQUEST['break']) ? $_REQUEST['break'] : 'medium';
    $article1 = isset($_REQUEST['article1']) ? $_REQUEST['article1'] : '';
    $article2 = isset($_REQUEST['article2']) ? $_REQUEST['article2'] : '';
    $boxqty = 0;
    $newdate = 0;
    $customerid = $_SESSION['customerid'];
    if ( $_SESSION['langpair'] == '' ) { $langpair = '0'; } else { $langpair = $_SESSION['langpair']; }
    $change_new = isset($_REQUEST['change_new']) ? $_REQUEST['change_new'] : '';
    $change_old = isset($_REQUEST['change_old']) ? $_REQUEST['change_old'] : '';
    if ( $change_new <> '' ) { require_once ('newlang.html'); }
    if ( $change_old <> '' ) { require_once ('oldlang.html'); }
    // Sprachen auslesen
    $sql6 = "SELECT cande,learnde,langpair FROM settings where customerid = '$customerid'";
    $db_sel6 = mysql_query_secure( $sql6 );
	while ($row=mysql_fetch_array($db_sel6)) {
    $cande = $row['cande'];
	$learnde = $row['learnde'];
	if ( $cande == "" or $learnde == "" or $cande == "-" or $learnde == "-" )
    {
		echo '<h2>Download</h2>';
        echo '<br />';
        $missinglangpair = '1';
        require_once ('sprache-waehlen.html');
    }
    else
    {
        // check, if maxqty is exeeded
        $sql = "select actqty, maxqty, sevendays from settings where customerid = ". $customerid;
        $sql2 = "select 1word from wordbox0 where customerid = ". $customerid . " and boxid = ". $_SESSION['actbox'] . " and langpairid = ". $langpair;
        $db_sel = mysql_query( $sql );
        if ( ! $db_sel ) { die('Error query: ' . mysql_error()); }
        $db_sel2 = mysql_query( $sql2 );
        if ( ! $db_sel2 ) { die('Error query: ' . mysql_error()); }
        $today = mktime(); // now
        $boxqty = mysql_num_rows($db_sel2);
        while ($row = mysql_fetch_array( $db_sel, MYSQL_ASSOC)) {
        $actqty = $row['actqty'];
        $sevendays = $row['sevendays']; // 7 days timelimit
        // convert into timestamp
        $hour = substr($sevendays,11,2);
        $minute = substr($sevendays,14,2);
        $second = substr($sevendays,17,2);
        $month = substr($sevendays,5,2);
        $day = substr($sevendays,8,2);
        $year = substr($sevendays,0,4);
        $mktime = mktime($hour, $minute, $second, $month, $day, $year);
        // difference between now and 7 days limit, rounding down
        $diff = round(($today-$mktime) / (3600*24),0); 
        //    floor($diff);
        if ( $diff >= 7 ) { $actqty = 0; $newdate = 1; }

		$rest = $row['maxqty'] - $actqty;
        if ( $actqty >= $row['maxqty'] )
		{
            $action = '';
            echo '<div class="message">Nur 100 Vokabeln Download pro Woche erlaubt und bereits '. $actqty . ' Vokabeln heruntergeladen. Zur Zeit kein Download möglich.</div>'; 
			
		}
		} 
		if (isset($_REQUEST['mp3_x']))       
		{	 
			$action = 'mp3'; 
		}
	

    switch ( $action ) {
        case 'mp3'   :     mp3_form(); break;
        case 'pdf' :     pdf_form(); break;
        default:           show_form(); pdf_form();
    }
}}
}

// ----------------------- Functions --------------------------------------------------------------------------------------------

function show_form() {

    $customerid = $_SESSION['customerid'];

    echo '<form action="download.html" method="post" >';
    echo '<h2>Vokabeln downloaden</h2>';
    echo '<br />';

    echo '<div class="content_frame">';
    echo 'Hier k&ouml;nnen die gew&auml;hlten Vokabeln einer Vokabelbox im Audioformat als <b>MP3-Files</b> herunter';
    echo 'geladen werden.<br />W&auml;hle dazu Deine pers&ouml;nlichen Einstellungen:<br />';
    echo '<br />';
    echo '<div class="left"><b>Pausenl&auml;nge:</b></div>';
    echo '<div class="right3" ><input type="radio" id="" name="break" value="medium" checked="checked" ></input>';
    echo '&nbsp;mittel / 3,5 s</div>';
    echo '<div class="right4" ><input type="radio" id="" name="break" value="long" ';
    echo '></input>';
    echo '&nbsp;lang / 4 s</div>';
    echo '<div class="right4" ><input type="radio" id="" name="break" value="short" ';
    echo '></input>';
    echo '&nbsp;kurz / 3 s</div>';
    echo '<br />';
    echo '<br />';
    
    $sql = "SELECT cande,learnde,langpair FROM settings where customerid = ". $customerid;
    //$sql3 = "SELECT distinct learn FROM langpairs_german order by langpairid";
    //$sql4 = "SELECT distinct can FROM langpairs_german order by langpairid";
    // DB Query
    $db_res = mysql_query( $sql );
    if ( ! $db_res ) { die('Error query1: ' . mysql_error()); }
    /*$db_sel3 = mysql_query( $sql3 );
    if ( ! $db_sel3 ) { die('Error query2: ' . mysql_error()); }
    $db_sel4 = mysql_query( $sql4 );
    if ( ! $db_sel4 ) { die('Error query3: ' . mysql_error()); }*/

    while ($row = mysql_fetch_array( $db_res, MYSQL_ASSOC)) {
    $langpair = $row['langpair'];
    $new = $row['learnde'];
    if ( $new == '' ) { $new = 'Sprache1'; }
    $old = $row['cande'];
    if ( $old == '' ) { $old = 'Sprache2'; }
    
    echo '<div class="left"><b>Artikel:</b></div>';
	
	echo '<table border="0" >';
    echo '<tr>';
    echo '<td class="table5">';
    echo '<input type="Checkbox" name="article1" value="language1">';
    echo '&nbsp;'. $new;
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td class="table5">';
    echo '<input type="Checkbox" name="article2" value="language2">';
    echo '&nbsp;'. $old;
    echo '</td>';
    echo '</tr>';
    echo '</table>';
    }
    
    echo '<br />';
    echo '<br />';
    echo '<h4>Download</h4>';
    echo '<input type="image" src="bilder/buttons/mp3.gif" name="mp3" alt="MP3" title=" MP3 " />';
    echo '</div>';
    echo '<br />';
    echo '</form>';
} 

function mp3_form() {

    global $break,$boxqty,$actqty,$newdate,$article1,$article2,$rest;

    $customerid = $_SESSION['customerid'];
	
	if ( $rest > 0 ) {	
    echo '<form action="mp3.html" method="post" >'; } else {
    echo '<form action="download.html" method="post" >'; } 
	
    echo '<input type=hidden name=custid value="';
    echo $customerid;
    echo '"/>';
    echo '<input type=hidden name=break value="';
    echo $break;
    echo '"/>';
    echo '<input type=hidden name=article1 value="';
    echo $article1;
    echo '"/>';
    echo '<input type=hidden name=article2 value="';
    echo $article2;
    echo '"/>';
    echo '<input type=hidden name=actqty value="';
    echo $actqty;
    echo '"/>';
    echo '<input type=hidden name=boxqty value="';
    echo $boxqty;
    echo '"/>';
    echo '<input type=hidden name=newdate value="';
    echo $newdate;
    echo '"/>';
    echo '<input type=hidden name=rest value="';
    echo $rest;
    echo '"/>';
    echo '<h2>Download</h2>';
    echo '<br />';
    echo '<div class="content_frame">';
    echo 'Um einen kostenlosen Service anbieten zu k&ouml;nnen, ist der Download auf <b>100</b> Vokabeln pro Woche eingeschr&auml;nkt.';
	echo '<br /><br />Die letzten 7 Tage bereits heruntergeladen:&nbsp;<b>'. $actqty . '</b>';
	echo '<br />Verbleibende Anzahl von Vokabeln:&nbsp;<b>'. $rest . '</b>';
    if ( $boxqty + $actqty > 100 ) {
	
    echo '<br /><br />Es können nur noch <b>'. $rest . '</b> Vokabel(n) heruntergeladen werden. Die Anzahl der ausgewählten Vokabeln wird automatisch auf <b>'. $rest . '</b> reduziert.
	Es werden dabei diejenigen Vokabeln gewählt, die innerhalb dieser Vokabelbox die längste Zeit nicht heruntergeladen wurden.'; 
	}
	echo '<br /><br /><input type="image" src="bilder/buttons/weiter.gif" name="mp3" alt="Weiter" title=" Weiter " />';
    echo '<br /><br />Die Anzahl der verf&uuml;gbaren Vokabeln sowie auch die Qualit&auml;t der bereits vertonten Vokabeln wird kontinuierlich verbessert.';
    echo '&nbsp;Viel Spa&szlig; beim Lernen!';
    echo '</div>';

    echo '</form>';
}

function pdf_form() {

    global $boxqty,$actqty,$rest;

    $customerid = $_SESSION['customerid'];
    $langpair = $_SESSION['langpair'];

	if ( $rest > 0 ) {	
    echo '<form action="pdf.html" method="post" >'; } else {
    echo '<form action="download.html" method="post" >'; } 
    echo '<div class="content_frame">';
    echo 'Auf Wunsch lassen sich die Vokabeln auch als <b>PDF-File</b> nachlesen oder ausdrucken:';
    echo '<br /><br />';
    echo '<h4>Download</h4>';
    echo '<input type=hidden name=custid value="';
    echo $customerid;
    echo '"/>';
    echo '<input type=hidden name=langpair value="';
    echo $langpair;
    echo '"/>';
    echo '<input type=hidden name=rest value="';
    echo $rest;
    echo '"/>';
    echo '<input type="image" src="bilder/buttons/pdf.gif" name="pdf" alt="PDF" title=" PDF " />';
    echo '</div>';
    echo '</form>';
}

function mysql_query_secure($query)
{
   $result=mysql_query($query);
   if ( ! $result ) { die('Error query : ' . mysql_error()); }
   return $result;
}


echo '</div>';
echo '</div>';
echo '<div id="adverts">';
echo '<div id="adbox"></div>';
echo '</div>';

require ('fuss.html');
?>

</body>
</html>